﻿using System;
using MapEditor.Windows.EditorCreateMap;
using MapEditor.Components;
using MapEditorXna;
using ZeldoEngineObjects.Maps;

namespace MapEditor.Windows.EditorWindows
{
    /// <summary>
    /// Méthode associées a la manipulation de cartes
    /// </summary>
    public partial class MainWindowsActions
    {
        public void AfficherCarte(IntPtr drawSurface)
        {
            this._mapSurface = new MapView(drawSurface);
            this._mapSurface.Run();
        }

        public void OuvrirFenetreNouvelleCarte()
        {
            var winNouvelleCarte = new MapNew();
            winNouvelleCarte.SubmitMap += this.InstancierCarte;
            winNouvelleCarte.ShowDialog();
        }

        public void InstancierCarte(MapModel mapSubmited)
        {
            if (mapSubmited != null)
            {
                AppParams.GetIns().Project.AjouterCarte(mapSubmited);

                var n = new TreeNodeMap(mapSubmited);
                n.ImageIndex = 1;

                this.windows.AjouterMapArbre(n);

                // Chargement de la carte pour affichage
                this._mapSurface.controller.LoadMap(mapSubmited);

                this.windows.ChargerTileset(mapSubmited);
                this.windows.ChargerAffichageCarte(mapSubmited);

                AppParams.GetIns().MapLoaded = true;
            }
        }

        public void ChargerCarte(TreeNodeMap treeMap)
        {
            this._mapSurface.controller.LoadMap(treeMap.Map);

            this.windows.ChargerTileset(treeMap.Map);
            this.windows.ChargerAffichageCarte(treeMap.Map);
        }

        public void DessinerTuile(int x, int y)
        {
            if (AppParams.GetIns().CurrentLayer != AppParams.Layer.LayerEvent)
            {
                var layerNumber = 0;

                switch (AppParams.GetIns().CurrentLayer)
                {
                    case AppParams.Layer.Layer1: layerNumber = 0; break;
                    case AppParams.Layer.Layer2: layerNumber = 1; break;
                    case AppParams.Layer.Layer3: layerNumber = 2; break;
                }

                var w = AppParams.GetIns().Selection.Width;
                var h = AppParams.GetIns().Selection.Height;
                var selectedTile = AppParams.GetIns().StartSelectedTile;

                this._mapSurface.controller.Click(x, y, w, h, selectedTile, layerNumber);
            }

            this.ChangerMapSelectionPosition(x, y, AppParams.GetIns().Selection.Width, AppParams.GetIns().Selection.Height);
        }

        public void ChangerMapSelectionPosition(int x, int y, int width, int height)
        {
            this._mapSurface.controller.SetCursorPosition(x, y, width, height);
        }

        public void ScrollerCarte(ScrollType scrollType, int valeur)
        {
            switch (scrollType)
            {
                case ScrollType.HScroll: this._mapSurface.controller.OffsetX = valeur; break;
                case ScrollType.VScroll: this._mapSurface.controller.OffsetY = valeur; break;
            }
        }
    }
}
